package com.qili.core.quartz.CustomQuartz;

import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.dto.SupervisorFileDTO;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.util.FilePathUtil;
import lombok.extern.java.Log;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author zhaoyongke
 * @date 2022/7/18
 * @description
 */

@Log
public class BigDxJob implements Job  {

    @Autowired
    SupervisorFileService supervisorFileService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("××××××××××××××××××××执行大小写转化×××××××××××××××××××××");
        SupervisorFileDTO  dto=new  SupervisorFileDTO();
        List<SupervisorFileDTO> supervisorFileDTOS =
                supervisorFileService.queryBigMall(dto);

        for (int i = 0; i <supervisorFileDTOS.size() ; i++) {
            SupervisorFileDTO supervisorFileDTO = supervisorFileDTOS.get(i);
            String id = supervisorFileDTO.getId();
            String url = supervisorFileDTO.getUrl();

            String fileSufiix = FilePathUtil.getFileSufiix(url);
            int i1 = url.lastIndexOf(fileSufiix);
            String substringurl = url.substring(0, i1);
            // old 文件
            File Oldname = new File(url);
            // xin文件
            String  filePathnew=substringurl+fileSufiix.toLowerCase();
            log.info("××××××××××××××××××××旧的路径×××××××××××××××××××××"+url);
            log.info("××××××××××××××××××××新的路径×××××××××××××××××××××"+filePathnew);
            File newName = new File(filePathnew);
            if(Oldname.renameTo(newName)) {
                System.out.println("已重命名");
            } else {
                System.out.println("Error");
            }
            // 更新路径
            SupervisorFile supervisorFile = supervisorFileService.selectByPrimaryKey(id);
            supervisorFile.setUrl(filePathnew);
            supervisorFileService.updateByPrimaryKey(supervisorFile);


        }



    }


}
